package com.twetec.cms.adapter.pc.backend.tag;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Maps;
import com.twetec.cms.adapter.pc.backend.tag.annotation.TplTag;
import com.twetec.cms.adapter.pc.backend.tag.doc.TagDocObject;
import com.twetec.cms.common.util.validate.ValidateUtil;
import com.twetec.cms.model.po.website.ColumnPO;
import com.twetec.cms.model.po.website.SiteColumnPO;
import com.twetec.cms.model.po.website.SitePO;
import com.twetec.cms.service.website.ColumnService;
import com.twetec.cms.service.website.SiteColumnService;
import com.twetec.cms.service.website.SiteService;
import org.beetl.core.tag.GeneralVarTagBinding;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service()
@Scope("prototype")
@TplTag("columnUrl")
public class ColumnUrlTag extends GeneralVarTagBinding {

	@Autowired
	private SiteService siteService;

	@Autowired
	private ColumnService columnService;

	@Autowired
	private SiteColumnService siteColumnService;

	@Override
	public void render() {
		System.out.println("ColumnUrlTag");

		// 接收标签参数
		Map result = Maps.newHashMap();
		// 栏目id
		Long columnId=  (this.getAttributeValue("columnId") instanceof String) ?
				Long.parseLong((String) this.getAttributeValue("columnId")) : (Long) this.getAttributeValue("columnId");

//		if (null == pageType || !pageType.equals("checkPage")) {

			// 获取栏目信息
			String url = "";
			url = p_getXpathEncodeUrl(columnId, url);
			result.put("url", url);

			this.binds(result);
			this.doBodyRender();
//		} else {
//			result.put("url", serverUrl + "website/column/check-column-page?columnId=" + columnId
//					+ "&siteId=" + siteId +"&page=1");
//			this.binds(result);
//			this.doBodyRender();
//		}
	}

	// xmszgh/shouye/ji_ceng_ban_shi/index.html 从后往前拼接
	private String p_getXpathEncodeUrl(Long columnId, String xpathEncodeUrl) {
		// 通过栏目id获取栏目信息
		ColumnPO columnPO = columnService.getById(columnId);
		xpathEncodeUrl =  "/" + columnPO.getEncoded() + xpathEncodeUrl;

		// 如果栏目的父级栏目是0 说明是顶级栏目-->通过栏目本身id去中间表查对应的站点信息
		if (columnPO.getParentId().equals(0L)) {
//			QueryWrapper<SiteColumnPO> siteColumnPOQueryWrapper = new QueryWrapper<>();
//			siteColumnPOQueryWrapper.eq("COLUMN_ID", columnPO.getId());
//			SiteColumnPO siteColumnPO = siteColumnService.getOne(siteColumnPOQueryWrapper);
//			// 得到对应的站点信息-->获取站点域名
//			SitePO sitePO = siteService.getById(siteColumnPO.getSiteId());
			// 直到拼接到顶级栏目时停止-->获取站点域名-->返回当前完整的url
//			return sitePO.getDomainUrl() + xpathEncodeUrl + "/index.html";
			return  xpathEncodeUrl + "/index.html";
		}

		// 如果父级栏目不是0(说明不是顶级栏目),则根据父级栏目id 重新进入方法查找对应的父级栏目的信息 再拼接url
		return p_getXpathEncodeUrl(columnPO.getParentId(), xpathEncodeUrl);
	}


	/**
	 * 标签文档
	 * @return
	 */
	public static TagDocObject doc() {
		TagDocObject tagDocObject = new TagDocObject();
		tagDocObject.setTagName("columnUrl");
		tagDocObject.setDesc("栏目地址标签");
		tagDocObject.setExample("<#columnUrl serverUrl=\"${serverUrl}\" columnId=\"1456134486191112194\" pageType=\"${pageType}\" dm=\"${site.dm!}\" siteId=\"${site.siteId}\" var=\"data\">" +
				"        <a href=\"${data.url!}\" target=\"_blank\">查看更多></a>\n" +
				"\t\t</#columnUrl>");

		tagDocObject.setParamDesc("columnId：栏目id\n" +
				"serverUrl：资讯发布系统的域名或IP和Port\n" +
				"siteId：站点id\n" +
				"dm：站点域名\n" +
				"pageType：用于标识是生成静态页面还是预览（预览：checkPage，生成：null或staticPage）\n" +
				"--------- var: 标签的数据结果集 ---------\n" +
				"url：跳转地址");

		tagDocObject.setOtherDesc("无");

		return tagDocObject;
	}

}
